package cn.ccit.util;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;

import com.mysql.jdbc.Statement;

import cn.ccit.entites.DiningTable;

public class MyDBUtil {
	private static final Log log = LogFactory.getLog(MyDBUtil.class);
	private static SessionFactory sessionFactory;
	private static Session session;
	static {
		final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
				.configure() // configures settings from hibernate.cfg.xml
				.build();
		
		try {
			sessionFactory = new MetadataSources(registry).buildMetadata()
					.buildSessionFactory();
		} catch (Exception e) {
			// The registry would be destroyed by the SessionFactory, but we had
			// trouble building the SessionFactory
			// so destroy it manually.
			e.printStackTrace();
			StandardServiceRegistryBuilder.destroy(registry);
		}
	}
	/**
	 * 获取SessionFactory对象
	 * @return
	 * @throws Exception
	 */
	private static SessionFactory getSessionFactory() throws Exception {
		// A SessionFactory is set up once for an application!

		return sessionFactory;
	}
	/**
	 * 获取会话对象
	 * @return
	 */
	public static Session getSession() {
		session= sessionFactory.openSession();
		return session;
	}
	/**
	 * 关闭会话
	 */
	public static void closeSession() {
		if(session!=null){
			session.close();
			session=null;
			
		}
	}
	/**
	 * 事务回滚
	 */
	public static void rollBack() {
		if(session!=null){
			session.getTransaction().rollback();
		}
	}
	
	/**
	 * 
	 * 方法功能说明：  修改对应的实体类的枚举值
	 * 创建：2016年11月13日 by Administrator   
	 * 修改：日期 by 修改者  
	 * 修改内容：  
	 * @参数： @param classname 传入实体类的名字
	 * @参数： @param data 传入的实体类对象
	 * @参数： @return      
	 * @return boolean     
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public static <T> boolean changeDataEnum(String classname,T data) {
		Class c=data.getClass();
		session =  MyDBUtil.getSession();
		if(session!=null){
			try {
				Method mgetId =c.getMethod("getId"),
						   mgetEnumName=c.getMethod("getEnumName"),
						   mgetEnumCode=c.getMethod("getEnumCode");
				session.beginTransaction();
				String hql="update "+classname+" obj set obj.enumCode=?,obj.enumName=?  where obj.id=?";
				
				Query query=session.createQuery(hql);
				int code=(Integer) mgetEnumCode.invoke(data);
				
				query.setParameter(0, code);
				String enumName =(String) mgetEnumName.invoke(data);
				
				query.setParameter(1, enumName);
				int id=(Integer) mgetId.invoke(data);
				
				query.setParameter(2, id);
				int state=query.executeUpdate();
				session.getTransaction().commit();
				if(state>0){
					return true;
				}
				} catch (Exception e) {
				e.printStackTrace();
				MyDBUtil.rollBack();
					
			}finally{
				session.close();
			}
		}
		return false;
	}
	
	public static boolean removeAllDataFromTable(String className,String col,String val) {
		String sql="delete from "+className+" where "+col+"="+val; 
		session =  MyDBUtil.getSession();
		try {
			session.beginTransaction();
			//获取connection,执行静态SQL
			Query query=session.createQuery(sql);
			int state=query.executeUpdate();
			session.getTransaction().commit();	
			if(state>0){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			rollBack();
		}finally{
			session.close();
		}
		return false;
	}
}
